package com.elvin.zoe.util;

import java.util.Calendar;
import java.util.Collection;
import java.util.Date;

import org.apache.commons.lang.time.DateUtils;

import com.elvin.zoe.pojo.structure.MessageCount;
import com.elvin.zoe.pojo.structure.MessageGroup;

public class StringUtil {

    public static String getTimeStringFromSeconds(int sec){
        if(sec <= 0)
            return "0\"";

        int hours = sec / 60 / 60;
        int mins = sec % (60*60) / 60;
        int secs = sec % (60*60) % 60;

        if(hours > 0)
            return hours + ":" + mins + "'" + secs + "\"";
        else if(mins > 0)
            return mins + "'" + secs + "\"";
        else
            return secs + "\"";
    }

    public static String escapeTextForHtml(String text){
        text = text.replace("'", "%27");
        text = text.replace("\"", "%22");
        text = text.replace(":", "%3a");
        return text;
    }

    public static int getLastDayOfMonth(MessageGroup monthMessageGroup){
    	Date month = monthMessageGroup.getMessageGroupDate();
    	return DateUtils.toCalendar(month).getActualMaximum(Calendar.DATE);
    }

    public static String getChartDataValue(MessageGroup monthMessageGroup){

    	StringBuffer buf = new StringBuffer();
    	StringBuffer smsBuf = new StringBuffer();
    	StringBuffer callBuf = new StringBuffer();
    	StringBuffer msnBuf = new StringBuffer();
    	StringBuffer axisBuf = new StringBuffer();

        Collection<MessageGroup> daylyMessageGrouplist = monthMessageGroup.getMessageGroups();
        int trackingDay =  -1;
        for(MessageGroup messageGroup : daylyMessageGrouplist){
        	Date dayDate = messageGroup.getMessageGroupDate();
        	int day = DateUtils.toCalendar(dayDate).get(Calendar.DAY_OF_MONTH);
        	if(trackingDay == -1){
        		trackingDay = day;
        	}
        	int gap = day - trackingDay;
			if (gap > 0)
				for (int i = 0 ; i < gap ; i++) {
					appendMessageCount(smsBuf, callBuf, msnBuf, axisBuf, trackingDay, new MessageCount());
		        	trackingDay ++;
				}
        	MessageCount dailyMessageCount = messageGroup.getMessageCount();
        	appendMessageCount(smsBuf, callBuf, msnBuf, axisBuf, day, dailyMessageCount);
        	trackingDay ++;
        }

        String axis = axisBuf.toString().substring(0, axisBuf.toString().length() - 1);
        String sms = smsBuf.toString().substring(0, smsBuf.toString().length() - 1);
        String call = callBuf.toString().substring(0, callBuf.toString().length() - 1);
        String msn = msnBuf.toString().substring(0, msnBuf.toString().length() - 1);
    	buf.append(axis).append("|")
    	   .append(sms).append("|")
    	   .append(axis).append("|")
    	   .append(call).append("|")
    	   .append(axis).append("|")
    	   .append(msn)
    	   ;

        return buf.toString();
    }

	private static void appendMessageCount(StringBuffer smsBuf, StringBuffer callBuf, StringBuffer msnBuf, StringBuffer axisBuf, int day, MessageCount dailyMessageCount) {
		smsBuf.append(dailyMessageCount.getSmsMessageCount()).append(",");
		callBuf.append(dailyMessageCount.getCallMessageDuration()).append(",");
		msnBuf.append(dailyMessageCount.getMsnMessageCount()).append(",");
		axisBuf.append(day).append(",");
	}

    public static String getChartDataScale(MessageGroupAnalyticsData messageGroupAnalyticsData, MessageGroup monthMessageGroup){

        int lastDay = getLastDayOfMonth(monthMessageGroup);
        String xLine = "1," + lastDay + ",";

        return xLine + "0," + messageGroupAnalyticsData.getMaxSmsCount() + ","
             + xLine + "0," + messageGroupAnalyticsData.getMaxCallCount() + ","
             + xLine + "0," + messageGroupAnalyticsData.getMaxMsnCount() ;
    }

    public static String getChartDataValueMarker(MessageGroupAnalyticsData messageGroupAnalyticsData){
        String template = "[0T],0000FF,0,[0],10,,l:5:0|[1T],FF0000,1,[1],10,,l:5:0|[2T],FF9900,2,[2],10,,l:5:0";

        return template.replace("[0T]", "N*")
                       .replace("[0]", messageGroupAnalyticsData.getMaxSmsIndex()+"")
                       .replace("[1T]", "t" + escapeTextForHtml(getTimeStringFromSeconds(messageGroupAnalyticsData.getMaxCallCount())))
                       .replace("[1]", messageGroupAnalyticsData.getMaxCallIndex()+"")
                       .replace("[2T]", "N*")
                       .replace("[2]", messageGroupAnalyticsData.getMaxMsnIndex()+"");
    }

    public static MessageGroupAnalyticsData getAnalyticsData(MessageGroup monthMessageGroup){
        int maxSmsCount = 0;
        int maxCallCount = 0;
        int maxMsnCount = 0;
        int maxSmsIndex = 0;
        int maxCallIndex = 0;
        int maxMsnIndex = 0;

        Collection<MessageGroup> daylyMessageGrouplist = monthMessageGroup.getMessageGroups();

        Date firstMessageDay = getFirstMessageDay(monthMessageGroup);
        int firstDayOfMonth = DateUtils.toCalendar(firstMessageDay).get(Calendar.DATE);
        for(MessageGroup messageGroup : daylyMessageGrouplist){
            MessageCount dailyMessageCount = messageGroup.getMessageCount();
            int index = DateUtils.toCalendar(messageGroup.getMessageGroupDate()).get(Calendar.DATE) - firstDayOfMonth ;

            maxSmsIndex = dailyMessageCount.getSmsMessageCount() > maxSmsCount ? index : maxSmsIndex;
            maxSmsCount = dailyMessageCount.getSmsMessageCount() > maxSmsCount ? dailyMessageCount.getSmsMessageCount() : maxSmsCount;

            maxCallIndex = dailyMessageCount.getCallMessageDuration() > maxCallCount ? index : maxCallIndex;
            maxCallCount = dailyMessageCount.getCallMessageDuration() > maxCallCount ? dailyMessageCount.getCallMessageDuration() : maxCallCount;

            maxMsnIndex = dailyMessageCount.getMsnMessageCount() > maxMsnCount ? index : maxMsnIndex;
            maxMsnCount = dailyMessageCount.getMsnMessageCount() > maxMsnCount ? dailyMessageCount.getMsnMessageCount() : maxMsnCount;
        }

        return new MessageGroupAnalyticsData(maxSmsCount,maxCallCount,maxMsnCount,maxSmsIndex,maxCallIndex,maxMsnIndex);
    }

    private static Date getFirstMessageDay(MessageGroup monthMessageGroup) {
        Date firstMessageDay = null;

        Collection<MessageGroup> daylyMessageGrouplist = monthMessageGroup.getMessageGroups();
        for (MessageGroup messageGroup : daylyMessageGrouplist) {
            if (firstMessageDay == null) {
                firstMessageDay = messageGroup.getMessageGroupDate();
                continue;
            }
            firstMessageDay = messageGroup.getMessageGroupDate().before(firstMessageDay) ? messageGroup.getMessageGroupDate() : firstMessageDay;
        }
        return firstMessageDay;
    }

}
